package top.yongyan0624.common.lang;

import lombok.Data;

/**
 * @author Yongyan
 * @description
 * @date 2021/5/10
 **/

@Data
public class ResultVO<T> {
    private Integer code;
    private T data;
    private String message;

    private ResultVO(T data) {
        this.code = 200;
        this.data = data;
        this.message = "success";
    }

    private ResultVO(String msg, T data) {
        this.code = 200;
        this.data = data;
        this.message = msg;
    }

    private ResultVO(CodeMsg codeMsg) {
        this.code = codeMsg.getCode();
        this.message = codeMsg.getMsg();
    }

    //通过构造方法只需要传data就可以
    public static <T> ResultVO<T> success(T data) {
        return new ResultVO<>(data);
    }

    public static <T> ResultVO<T> success(String msg ,T data) {
        return new ResultVO<>(msg, data);
    }

    // 这里使用codeMsg 是因为返回结果的是code码不是固定的
    public static <T> ResultVO<T> error(CodeMsg codeMsg) {
        if (codeMsg == null) {
            System.out.println("codeMsg is null");
            return null;
        }
        return new ResultVO<T>(codeMsg);
    }
}
